﻿namespace _001_SingleResponsibilityPrinciple
{

    /*
     *  单一责任原则
     *  不同的类做不同的任务
     */
    internal class Program
    {
        static void Main(string[] args)
        {

            var j = new Journal();
            j.Add("hello world");
            j.Add("Fuck U");

            var persistence = new Persistence();
            persistence.SaveToFile("./a.txt",j);

        }
    }


    
    public class Journal
    {
        private readonly List<string> _entries = new List<string>();

        private static int _count = 0;


        public int Add(string entry)
        {
            _entries.Add($"{++_count}:{entry}");

            return _count;
        }

        public override string ToString()
        {
            return string.Join(Environment.NewLine, _entries);
        }
    }

    public class Persistence
    {
        public void SaveToFile(string filename, Journal journal,bool overWrite = false)
        {
            if (overWrite || !File.Exists(filename))
            {
                File.WriteAllText(filename,journal.ToString());
            }
        }
    }


}
